//
//  FansListViewController.m
//  UniversalApp
//
//  Created by 志刚杨 on 2018/3/1.
//  Copyright © 2018年 voidcat. All rights reserved.
//

#import "FansBaseViewController.h"
#import "FansModel.h"
#import "FansCollectionViewCell.h"
#import "UserInfoViewController.h"
@interface FansBaseViewController ()<FansLogicDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong) FansLogic *logic;//逻辑层

@end

@implementation FansBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化逻辑类
    _logic = [FansLogic new];
    _logic.delegagte = self;
    _logic.type = self.type;
    [_logic loadData];
    [self setupUI];
}
#pragma mark ————— 初始化页面 —————
-(void)setupUI{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView.collectionViewLayout = layout;
    layout.minimumLineSpacing = 1;//设置最小行间距
    layout.minimumInteritemSpacing = 1;//item间距(最小值)
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);//设置section的编距
    layout.headerReferenceSize=CGSizeMake(self.view.frame.size.width, 0.01); //设置collectionView头视图的大小
    
    self.collectionView.backgroundColor = CViewBgColor;
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([FansCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([FansCollectionViewCell class])];
        self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    
    if(self.type == CYFansTypeFans)
    {
        self.title = ASLocalizedString(@"粉丝");
    }
    else
    {
        self.title = ASLocalizedString(@"关注");
    }
}

#pragma mark ————— 下拉刷新 —————
-(void)headerRereshing{
    _logic.page = 0;
    [_logic loadData];
    [self removeNoDataImage];
    
}

#pragma mark ————— 上拉刷新 —————
-(void)footerRereshing{
    _logic.page+=1;
    [_logic loadData];
    [self removeNoDataImage];
}

#pragma mark ————— 数据拉取完成 渲染页面 —————
-(void)requestDataCompleted{
    [self.collectionView.mj_footer endRefreshing];
    [self.collectionView.mj_header endRefreshing];
    
    [UIView performWithoutAnimation:^{
        [self.collectionView reloadData];
    }];
    
}

#pragma mark ————— collection代理方法 —————
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _logic.dataArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    FansCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([FansCollectionViewCell class]) forIndexPath:indexPath];
    cell.backgroundColor = CF6;
    [cell setModel:_logic.dataArray[indexPath.row]];
    return cell;
    
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    FansModel *model = self.logic.dataArray[indexPath.row];
    
    UserInfoViewController *user = [UserInfoViewController new];
    user.uid = StringFromInt(model.id);
    [self.navigationController pushViewController:user animated:YES];
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    if (section == 0 )
    {
        return UIEdgeInsetsMake(0, 0, 0, 0);
    }
    
    return UIEdgeInsetsMake(0, 0, 0, 0);
}


- (CGSize)collectionView:(nonnull UICollectionView *)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    return CGSizeMake(SCREEN_WIDTH,60);
}

-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
    self.collectionView.frame = CGRectMake(0, 0, KScreenWidth, SCREEN_HEIGHT - TabBarHeight - 40);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
